<cfset BASE = CreateObject("component", "#session.rootDir#.cfc.base") />
<cfset mainQueries = CreateObject("component", "#session.rootDir#.cfc.queries") />
<cfset termQueries = CreateObject("component", "#session.rootDir#.cfc.terminationQueries") />
<cfset statusQueries = CreateObject("component", "#session.rootDir#.cfc.statusQueries") />
<cfset term = CreateObject("component", "#session.rootDir#.cfc.Termination") />
<cfinclude template="/code_lib/SCF_Employee.cfm">
<cfinclude template="/code_lib/SCF_HR.cfm">
<cfinclude template="/code_lib/SCF_REGEX.cfm">
<cfoutput>
<!---HTML header, css links, js links, and page title--->
#BASE.htmlTerminationHeader(
    "", <!---JS links--->
    "", <!---CSS links--->
    "Step 3: Scheduled Employee Termination", <!---Page Title--->
    "Status" <!---Active Nav Link--->
)#
<!---HTML header, css links, js links, and page title--->

<cfset ErrorInstructions = "">

        
<cfif (Session.bolAuth EQ True and (Session.strAuthType EQ Session.IT OR Session.strAuthType EQ Session.HR OR Session.strUserID EQ Session.SERVICE_ACCOUNT))>	

    <cfset QUERIES.GetScheduledTerms = termQueries.funcScheduledTerms( DateFormat(now(), "mm/dd/yyyy") ) />
    
	<cfset Summary = 'Scheduled Archive.  <BR/>Started:  #dateFormat(Now(), "mm/dd/yyyy")# #TimeFormat(Now(), "hh:mm:ss tt")#<BR /><hr /><br />'  >
    
    <cfloop query="QUERIES.GetScheduledTerms">        
        <cfset QUERIES.GetTermDetails = mainQueries.funcGetTermDetails( QUERIES.GetScheduledTerms.TERMREQID, 23 ) />
        <cfset QUERIES.GetEmpInfoByStep = mainQueries.funcGetEmpInfoTermination( QUERIES.GetTermDetails.ActionStepID ) />
        <cfset Step = "" />
        <cfset Error = "" />
        <cfset objEmp = SCF_Employee_GetEmployee(QUERIES.GetTermDetails.RosettaID) />
        <cfset objRequestor = SCF_Employee_GetEmployee(QUERIES.GetTermDetails.InitialTermReqByID) />
        <cfset SupID = SCF_HR_WhosTheBoss(QUERIES.GetTermDetails.RosettaID) />
        <cfset objSup = SCF_Employee_GetEmployee(SupID) />
        <cfset Summary &= 'User:  #objSup.FullName# (#objEmp.ExtendedLogin#)<BR/> Started: #dateFormat(Now(), "mm/dd/yyyy")# #TimeFormat(Now(), "hh:mm:ss tt")#<BR/>' />
        <cftry>
        <cfsetting enablecfoutputonly="true">
        <cfset Termination = term.init(QUERIES.GetScheduledTerms.TermReqID) />
        
        <!--- 1)  AD Disable --->
        <cfset Step = "Step 1 - AD Disable" />
        <cfset ADTerm = Termination.ADDisable() />
        <cfset ADResult = ADTerm.Message />
        <cfif ADTerm.STATUS EQ "Completed" >
            <cfset ADResult = ADTerm.Message />
        <cfelse>
            <cfset ErrorInstructions &= "Effective immediately, please disable the account for the following employee:  #objSup.FullName# (#objEmp.ExtendedLogin#)<Br />
                <PRE>
                Please take the following steps:<BR />
                1) Disable the Account
                2) Scramble the PW
                3) Hide from Address List (under 'Exchange Advanced')
                <Br /><Br />
                Error Text:  #ADResult#<BR/><BR/>
                </PRE><hr />"
            />
        </cfif>
        <!--- END 1 - AD Disable --->
        
        
        <!---2)  Home Folder Archive --->
        <cfset Step = "Step 2 - Home Folder Archive" />
        <cfset HomeFolderTerm = Termination.HomeFolderArchive() />
        <cfset HomeFolderResult = HomeFolderTerm.Message />
        <cfif HomeFolderTerm.STATUS EQ "Completed" >            
            <cfset HomeFolderResult = HomeFolderTerm.Message />
        <cfelse>         
            <cfset ErrorInstructions &= "Error Archiving Home Folder for  #objSup.FullName# (#objEmp.ExtendedLogin#)<Br /><Br />
                <PRE>
                Please take the following steps: <BR />
                1) Manually Archive the home directory <Br />
                Error Text:  #HomeFolderResult# <BR/><BR/>
                </PRE><hr />" 
            />                    
        </cfif>
        <!---END 2 - Home Folder Archive --->
        
        <!---3) Mailbox Export --->
        <cfset randomnum = RAND()*10 />
        <cfthread name="MailboxExport#randomnum#" action="run" >
	        <cfset Step = "Step 3 - Mailbox Export" />
	        <cfset MailTerm = Termination.EmailExport() />
	        <cfset MailResult = MailTerm.Message />
	        <cfif MailTerm.STATUS EQ "Completed" >            
	            <cfset MailResult = MailTerm.Message />
	        <cfelse>         
	            <cfset ThisErrorInstructions = "Error Exporting the Mailbox for #objSup.FullName# (#objEmp.ExtendedLogin#)<Br /><Br />
		            <PRE>
		            Please take the following steps:<BR />
		            1) Manually Export the Mailbox
		            2) Place the .PST file in the user's archived home folder<Br />
		            Error Text:  #MailResult# <BR/><BR/>
		            </PRE><hr />" 
	            />
                <cfmail to=#Session.SERVICEDESK_EMAIL# From="EPAR_Error@scf.cc" subject="EPAR Scheduled Termination Mailbox Export Error: #objEmp.ExtendedLogin#" type="html">
                #ThisErrorInstructions#
                </cfmail>                   
	        </cfif>            
        </cfthread>
        
                 
        <!---END 3 - Mailbox Export --->
        
        <!---4) AD Archive User Account  --->   
        <cfset Step = "Step 4 - AD Archive User Account" />
        <cfset AccountArchiveTerm = Termination.UserAccountDisable() />
        <cfset AccountArchiveResult = AccountArchiveTerm.Message />
        <cfif AccountArchiveTerm.STATUS EQ "Completed" >            
            <cfset AccountArchiveResult = AccountArchiveTerm.Message />
        <cfelse>         
            <cfset ErrorInstructions &= "Error Archiving the User Account for #objSup.FullName# (#objEmp.ExtendedLogin#)<Br /><Br />
	            <PRE>
	            Please take the following steps:<BR />
	            1) Manually disable acocunt
	            2) Move to the Archived_Users container<Br />
	            Error Text:  #AccountArchiveResult# <BR/><BR/>
	            </PRE><hr />" 
            />                    
        </cfif>         
        <!---END 4 - AD Archive User Account --->
        
        <!---5) Disable Rosetta  --->
        <cfset Step = "Step 5 - Disable Rosetta" />
        <cfset RosettaTerm = Termination.RosettaDisable() />
        <cfset RosettaResult = RosettaTerm.Message />
        <cfif RosettaTerm.STATUS EQ "Completed" >            
            <cfset RosettaResult = RosettaTerm.Message />
        <cfelse>         
            <cfset ErrorInstructions &= "Error disabling rosetta for #objSup.FullName# (#objEmp.ExtendedLogin#)<Br /><Br />
                    <PRE>
                    Please take the following steps:<BR />
                    1) Contact App Dev to complete<Br />
                    Error Text:  #RosettaResult# <BR/><BR/>
                    </PRE><hr />" />
                    
        </cfif>             
        <!---END 5 - Disable Rosetta  --->
        
        <!---Final Clean up and Advance  --->
        <cfset Summary &= 'Results:<PRE>
            AD:  #ADResult#
            Home Folder Archive:  #HomeFolderResult#
            Mailbox Export: Thread Started
            Archive Account: #AccountArchiveResult#
            Disable Rosetta: #RosettaResult#
            </PRE>' />
        
        <cfset Summary &= 'Completed: #dateFormat(Now(), "mm/dd/yyyy")# #TimeFormat(Now(), "hh:mm:ss tt")#<BR /><hr /><br />'>
        <cfset termQueries.funcUpdateScheduledTerms( QUERIES.GetScheduledTerms.ID ) />
        
        <cfcatch type="any">
                <cfset Error &= ' Message: #CFCATCH.message#<BR />Detail: #CFCATCH.Detail#'>                
                <cfset mainQueries.funcInsertErrorLog(QUERIES.GetTermDetails.ActionStepID, -19, 'Error in Scheduled Termination/Layoff #Step#: #Error#')/>                
                <cfset Summary &= 'Error:  #Error#<BR/> Step: #Step#<BR /> Other Instructions: #ErrorInstructions#<BR />'>                    
                <cfmail to=#Session.SERVICEDESK_EMAIL# From="EPAR_Error@scf.cc" subject="EPAR Scheduled Termination Error: #objEmp.ExtendedLogin#" type="html">
                    Error in Scheduled Termination/Layoff #Step#<BR /> 
                    Error Text: #Error#<BR /> 
                    Employee: #objEmp.ExtendedLogin#<BR /> 
                    Step: #Step#<BR />
                    Action by User: Service account (scheduled)
                </cfmail>
            </cfcatch>        
        </cftry>
    </cfloop>
    
    
        
    <cfif ErrorInstructions NEQ "">
	    <cfmail to=#Session.SERVICEDESK_EMAIL# From="EPAR_Error@scf.cc" subject="EPAR Scheduled Termination Error(s)" type="html">
	        #ErrorInstructions#
	    </cfmail>	
	    #ErrorInstructions#
	    <br /><br />        
    <cfelse>         
	    #Summary# 
	    <cfmail to=#Session.ITS_EMAIL# From="EPAR_Scheduled@scf.cc" subject="EPAR Scheduled Termination Summary" type="html">
	        #Summary#
	    </cfmail>              
    </cfif>

   
<cfelse> <!--- Authorization Check --->
	<cfinclude template="../Denied.cfm">	
</cfif>


<!---HTML footer, closes html and body tags.  Any footer info goes here--->
#BASE.htmlTerminationFooter()#
<!---HTML footer, closes html and body tags.  Any footer info goes here--->
</cfoutput>